%{
#include <stdio.h>
#include <string.h>
extern FILE * yyin;
%}


%union
 {
  int number;
  char *string;
 }



%token <string> WORD
%token INT UNSIGNED TYPEDEF STRUCT DOUBLE LONG CONST CHAR FLOAT STAR DSTAR LB RB LP RP LSB RSB SLASH BACKSLASH SEMICOLON
%token VOID COMMA

%%

All:
|
All STRUCT WORD statement  {printf("struct name is %s\n",$3);}
;

statement:
LB expressions RB SEMICOLON
;

expressions:
|expression SEMICOLON 
|expressions expression SEMICOLON
;

expression:
|Type WORD           {printf("printf(\"%%d\",u -> %s);\n",$2);} 
|Type STAR WORD      {printf("printf(\"%%d\",*(u -> %s));\n",$3);}
|Type DSTAR WORD     {printf("printf(\"%%s\",*(u -> %s));\n",$3);}
|UNSIGNED Type WORD  {printf("printf(\"%%d\",u -> %s);\n",$3);}
|UNSIGNED Type STAR WORD  {printf("printf(\"%%d\",*(u -> %s));\n",$4);}
|CONST Type STAR WORD  {printf("printf(\"%%d\",*(u -> %s));\n",$4);}
|Type LP FunctionName RP LP Para RP
|Type WORD LSB WORD RSB {printf("Array Name is %s with Size %s\n",$2,$4);}
|Type STAR WORD LSB WORD RSB {printf("Array Name is %s with Size %s\n",$3,$5);}
|Type DSTAR WORD LSB WORD RSB {printf("Array Name is %s with Size %s\n",$3,$5);}


;

Type:
LONG
|INT
|DOUBLE
|CHAR
|VOID
|FLOAT
|WORD
;

FunctionName:
WORD {printf("functionname is: %s\n",$1);}
| STAR WORD {printf("functionname is: %s\n",$2);}
;

Para:
|expression
|Para COMMA expression
;
/*NUM
| expression '+' expression {$$ = $1 + $3;}
| expression '-' expression {$$ = $1 - $3;}
| expression '*' expression {$$ = $1 * $3;}
| expression '/' expression {$$ = $1 / $3;}
| '(' expression ')' {$$ = $2;}*/

%%

int yywrap()
{

  return 1;
}

int main(void) {
  int yydebug = 1;
  freopen("test1.txt","w",stdout);   
  yyin = fopen("test.txt","r");
  yyparse();
  fclose(yyin);
  fclose(stdout);
  return 0;
}
